package face.pay.task.card.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.wxpay.sdk.WXPayUtil;
import face.pay.constant.CardMessageType;
import face.pay.dao.CpnCardDetailInfoDao;
import face.pay.dao.CpnCardMessageDao;
import face.pay.dao.CpnMarketInfoDao;
import face.pay.entity.CpnCardDetailInfo;
import face.pay.entity.CpnCardMessage;
import face.pay.entity.CpnMarketInfo;
import face.pay.task.card.service.ICpnCardMessageService;
import face.pay.util.DateUtil;
import face.pay.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname CpnCardMessageServiceImpl
 * @Description Administrator
 * @Date 2019/12/16 12:00
 */
@Service
@Slf4j
public class CpnCardMessageServiceImpl implements ICpnCardMessageService {

    @Autowired
    private CpnCardMessageDao cpnCardMessageDao;

    @Autowired
    private CpnCardDetailInfoDao cpnCardDetailInfoDao;

    @Autowired
    private CpnMarketInfoDao marketInfoDao;

    /**
     * 推送类型为：用户领取优惠券
     * <p>
     * type 消息类型 : GETCARD 用户领取优惠券
     * CONSUME 核销优惠券
     *
     * @return
     */
    @Override
    public void pushGetCardMessage() {
        try {
            String type = CardMessageType.CARDGET;
            String nowTime = DateUtil.getNowTime();
            log.info("开始推送用户领取优惠券消息！");

            List<CpnCardMessage> cpnCardMessages = cpnCardMessageDao.queryGetCardMessage(type, nowTime);
            log.info("消息条数：" + cpnCardMessages.size());
            for (CpnCardMessage cardMessage : cpnCardMessages) {

                log.info("消息id:" + cardMessage.getId() + "类型：" + cardMessage.getType());
                CpnCardDetailInfo cpnCardDetailInfo = cpnCardDetailInfoDao.getById(cardMessage.getCardDetailId());
                CpnMarketInfo marketInfo = marketInfoDao.getById(cpnCardDetailInfo.getMarketId());

                String security_key = marketInfo.getSecurity_key();
                String market_code = marketInfo.getMarket_code();

                Map<String, String> signMap = new HashMap<>();
                signMap.put("market_code", market_code);
                String nonceStr = WXPayUtil.generateNonceStr();
                signMap.put("nonceStr", nonceStr);
                String sign = WXPayUtil.generateSignature(signMap, security_key);

                Map<String, Object> paramMap = new HashMap<>();
                Map<String, Object> data = new HashMap<>();
                Map<String, String> body = new HashMap<>();
                body.put("bizCode", cpnCardDetailInfo.getBizCode());
                data.put("body", body);
                data.put("type", cardMessage.getType());
                paramMap.put("data", data);
                paramMap.put("sign", sign);
                paramMap.put("nonceStr", nonceStr);

                String jsonData = JSON.toJSONString(paramMap);
                log.info("请求参数：" + jsonData);

                String responseStr = HttpClientUtil.doPostJson(marketInfo.getMessageUrl(), jsonData);
                log.info("请求成功，响应结果：" + responseStr);

                cardMessage.setCount(cardMessage.getCount() + 1);
                if ("SUCCESS".equals(responseStr)) {
                    cardMessage.setStatus(1);
                } else {
                    cardMessage.setNextTime(cardMessage.createNextTime());
                    cardMessage.setStatus(-1);
                }

                cpnCardMessageDao.update(cardMessage);
            }
            log.info("推送结束");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 推送用户核销完优惠券消息
     */
    @Override
    public void pushConsumeMessage() {
        try {


            String type = CardMessageType.CONSUME;

            String nowTime = DateUtil.getNowTime();

            log.info("开始推送用户使用优惠券并核销完的消息！");

            List<CpnCardMessage> cpnCardMessages = cpnCardMessageDao.queryConsumeMessage(type, nowTime);
            log.info("消息条数：" + cpnCardMessages.size());
            for (CpnCardMessage cardMessage : cpnCardMessages) {

                log.info("消息id:" + cardMessage.getId() + "类型：" + cardMessage.getType());
                CpnCardDetailInfo cpnCardDetailInfo = cpnCardDetailInfoDao.getById(cardMessage.getCardDetailId());
                CpnMarketInfo marketInfo = marketInfoDao.getById(cpnCardDetailInfo.getMarketId());

                String security_key = marketInfo.getSecurity_key();
                String market_code = marketInfo.getMarket_code();

                Map<String, String> signMap = new HashMap<>();
                signMap.put("market_code", market_code);
                String nonceStr = WXPayUtil.generateNonceStr();
                signMap.put("nonceStr", nonceStr);
                String sign = WXPayUtil.generateSignature(signMap, security_key);

                Map<String, Object> paramMap = new HashMap<>();
                Map<String, Object> data = new HashMap<>();
                Map<String, String> body = new HashMap<>();

                body.put("money",String.valueOf(cardMessage.getMoney()));
                body.put("bizCode", cpnCardDetailInfo.getBizCode());

                data.put("body", body);
                data.put("type", cardMessage.getType());
                paramMap.put("data", data);
                paramMap.put("sign", sign);
                paramMap.put("nonceStr", nonceStr);

                String jsonData = JSON.toJSONString(paramMap);
                log.info("请求参数：" + jsonData);

                String responseStr = HttpClientUtil.doPostJson(marketInfo.getMessageUrl(), jsonData);
                log.info("请求成功，响应结果：" + responseStr);

                cardMessage.setCount(cardMessage.getCount() + 1);
                cardMessage.setUrl(marketInfo.getMessageUrl());
                if ("SUCCESS".equals(responseStr)) {
                    cardMessage.setStatus(1);
                } else {
                    cardMessage.setNextTime(cardMessage.createNextTime());
                    cardMessage.setStatus(-1);
                }

                cardMessage.setUrl(marketInfo.getMessageUrl());
                cpnCardMessageDao.update(cardMessage);
            }
            log.info("推送结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
